package com.yc.cloud.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.entity.Company;
import com.yc.cloud.mapper.CompanyMapper;
import com.yc.cloud.portal.service.CompanyService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 企业 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-01-11
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {


    @Override
    public IPage<Company> list(BasePageReqDTO queryParam) {
        val query = new QueryWrapper<Company>();
        IPage<Company> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BasePageReqDTO.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(Company.ID);
            } else if (BasePageReqDTO.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(Company.ID);
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        return baseMapper.selectPage(page, query);
    }

    @Override
    public Company create(Company param) {
        val company = new Company();
        BeanUtils.copyProperties(param, company);
        val saveFlag = save(company);
        return saveFlag ? company : null;
    }

    @Override
    public Company get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public Company update(Long id, Company param) {
        val company = new Company();
        BeanUtils.copyProperties(param, company);
        company.setId(id);
        val updateFlag = updateById(company);
        return updateFlag ? company : null;
    }


    @Override
    public Company convertVo(Company company) {
        if (company == null) {
            return null;
        }
        val vo = new Company();
        BeanUtils.copyProperties(company, vo);
        return vo;
    }
}
